import { Component, ElementRef, OnInit, Input, ViewChild, Output, EventEmitter, AfterViewInit } from '@angular/core';
import { Service } from '../../server/server';
import { Pagination } from '../../public/serve/pcrudInfo';
import { JournalTableComponent } from '../journal/journal-table/journal-table.component';
import { BsDatepickerConfig } from 'ngx-bootstrap/datepicker';
// import { listLocales } from 'ngx-bootstrap/bs-moment';
import 'moment/locale/zh-cn';
import * as moment from 'moment';
import { CudTableService } from '../../public/serve/cud-table/cud-table.service';
declare var $: any;

@Component({
	selector: 'app-journal',
	templateUrl: './journal.component.html',
})
export class JournalComponent implements OnInit {

	tableNumber: number = 0;
	right: any = { "system": {}, "journal": {} };
	constructor(private server: Service, private tableListService: CudTableService) {
		this.right = $.extend(this.right, server.getRight());
	}

	public isActive = this.server.isActive;

	locale = 'zh-cn';
	// locales = listLocales();
	ngOnInit() {
		this.server.eventEmit.subscribe((value: any) => {
			this.isActive = value;
		});
		moment.locale('zh-cn');

		this.maxDate = new Date();
		this.maxDate.setDate(this.maxDate.getDate());

		$('#upsearch').keydown((e) => {
			if (e.keyCode == 13) {
				this.search();
			}
		});
	}

	public maxDate: Date;
	ngDoCheck() {
		this.right = $.extend(this.right, this.server.getRight());
	}

	//导入table
	@ViewChild("table")
	table: JournalTableComponent;

	// 表格
	public totalItems: number;
	ParentList(totalItems: number) {
		this.totalItems = totalItems;
	}

	// 当前页（默认为第一页）
	public pageNumber: number = 1;
	// 每页显示多少条数据（默认每页显示20条）
	public pageSize: number = 20;
	// id
	public id: string = '0';
	// 分页
	public pagination: Pagination;
	isLoadTableFromPaginationHandler(pagination: Pagination) {
		this.pagination = pagination;

		this.pageNumber = this.pagination[0].pageNumber;
		this.pageSize = this.pagination[0].pageSize;
		this.id = this.pagination[0].id;

		this.tableNumber = (pagination[0].pageNumber - 1) * pagination[0].pageSize;

		this.table.getTabList(
			this.pageNumber,
			this.pageSize,
			this.server.formatDate(this.beginCreateDate) || '',
			this.server.formatDate(this.endCreateDate) || '',
			this.status,
			this.queryCriteria,
			3
		);
	}

	//查询条件
	public queryCriteria: string = '';
	public status: number = 0;
	public endCreateDate: any = '';
	public beginCreateDate: any = '';



	//查询
	search() {
		this.table.getTabList(
			this.pageNumber,
			this.pageSize,
			this.server.formatDate(this.beginCreateDate) || '',
			this.server.formatDate(this.endCreateDate) || '',
			this.status,
			this.queryCriteria,
			3
		);
	}
	//监听
	changesearch() {
		this.search();
	}

}